local OHConfig = {}
local OptionHouse
local L = {
	["ENABLE_PERFORMANCE"] = "Enable performance tab",
	["ENABLE_MANAGEMENT"] = "Enable master control panel tab",
	["PERFORMANCE_SIZE"] = "Performance font size",
	["MANAGEMENT_SIZE"] = "Management font size",
	["ASK_ALWAYS"] = "Ask every time",
	["NO_ALWAYS"] = "Never enable",
	["YES_ALWAYS"] = "Always enable",
	["DEP_ENABLE"] = "Dependency enabling from the management panel",
	["CHILDREN_ENABLE"] = "Enable children addons from management panel",
	["GENERAL"] = "General",
}

function OHConfig:Enable()
	OptionHouse = DongleStub("OptionHouse-1.0")
	local OHObj = OptionHouse:RegisterAddOn("OptionHouse", nil, nil, "r" .. (tonumber(string.match("$Revision: 564 $", "(%d+)")) or 1))
	
	OHObj:RegisterCategory(L["GENERAL"], self, "CreateUI")
	
	if( not OptionHouseDB ) then
		OptionHouseDB = {}
	end
	
	self.defaults = {
		enablePerf = true,
		enableManage = true,
		dependMode = "yes",
		childMode = "ask",
		perfFontSize = 10,
		manageFontSize = 10,
	}

	self.db = setmetatable(OptionHouseDB, {
		__index = function(t, k)
			return self.defaults[k]
		end
	})
	
	if( self.db.enableManage ) then
		OHManage:Register()
	end

	if( self.db.enablePerf ) then
		OHPerformance:Register()
	end
end

function OHConfig:CreateUI()
	local frame = CreateFrame("Frame", nil)
	frame:SetScript("OnShow", function()
		OHConfig.perfCheck:SetChecked(OHConfig.db.enablePerf)
		OHConfig.manageCheck:SetChecked(OHConfig.db.enableManage)
		OHConfig.perfSize:SetText(OHConfig.db.perfFontSize)
		OHConfig.manageSize:SetText(OHConfig.db.manageFontSize)
	end )

	-- ENABLE PERFORMANCE TAB
	self.perfCheck = CreateFrame("CheckButton", "OHConfigUIPerformance", frame, "OptionsCheckButtonTemplate")
	self.perfCheck:SetWidth(32)
	self.perfCheck:SetHeight(32)
	self.perfCheck:SetPoint("TOPLEFT", 5, -5)
	OHConfigUIPerformanceText:SetText(L["ENABLE_PERFORMANCE"])
	self.perfCheck:SetScript("OnClick", function(self)
		if( self:GetChecked() ) then
			OHConfig.db.enablePerf = true
			OHPerformance:Register()
		else
			OHConfig.db.enablePerf = false
			OHPerformance:Unregister()
		end
	end )	

	-- FONT SIZE FOR PERFORMANCE
	self.perfSize = CreateFrame("EditBox", "OHConfigUIPerfSize", frame, "InputBoxTemplate")
	self.perfSize:SetAutoFocus(false)
	self.perfSize:SetHeight(20)
	self.perfSize:SetWidth(35)
	self.perfSize:SetPoint("TOPLEFT", 15, -40)
	self.perfSize:SetScript("OnTextChanged", function(self)
		OHConfig.db.perfFontSize = tonumber(self:GetText()) or 0
		OHPerformance:Reload()
	end)
	
	local text = self.perfSize:CreateFontString("OHConfigUIPerfSizeText", self.perfSize, "GameFontNormalSmall")
	text:SetPoint("LEFT", self.perfSize, "RIGHT", 5, 0)
	text:SetText(L["PERFORMANCE_SIZE"])

	-- ENABLE MANAGEMENT TAB
	self.manageCheck = CreateFrame("CheckButton", "OHConfigUIManage", frame, "OptionsCheckButtonTemplate")
	self.manageCheck:SetWidth(32)
	self.manageCheck:SetHeight(32)
	self.manageCheck:SetPoint("TOPLEFT", 5, -70)
	OHConfigUIManageText:SetText( L["ENABLE_MANAGEMENT"])
	self.manageCheck:SetScript("OnClick", function(self)
		if( self:GetChecked() ) then
			OHConfig.db.enableManage = true
			OHManage:Register()
		else
			OHConfig.db.enableManage = false
			OHManage:Unregister()
		end
	end)

	-- FONT SIZE FOR MANAGEMENT
	self.manageSize = CreateFrame("EditBox", "OHConfigUIManageSize", frame, "InputBoxTemplate")
	self.manageSize:SetAutoFocus(false)
	self.manageSize:SetHeight(20)
	self.manageSize:SetWidth(35)
	self.manageSize:SetPoint("TOPLEFT", 15, -105)
	self.manageSize:SetScript("OnTextChanged", function(self)
		OHConfig.db.manageFontSize = tonumber(self:GetText()) or 0
		OHManage:Reload()
	end)
	
	local text = self.manageSize:CreateFontString("OHConfigUIManageSizeText", self.manageSize, "GameFontNormalSmall")
	text:SetPoint("LEFT", self.manageSize, "RIGHT", 5, 0)
	text:SetText(L["MANAGEMENT_SIZE"])
	
	-- DROPDOWN FOR DEPENDENCY
	self.dependMode = CreateFrame("Frame", "OHConfigUIDepMode", frame, "UIDropDownMenuTemplate")
	self.dependMode:SetPoint("TOPLEFT", -10, -135)
	self.dependMode:SetScript("OnShow", function(self)
		UIDropDownMenu_Initialize(self, OHConfig.InitDepDropdown)
		UIDropDownMenu_SetSelectedValue(OHConfigUIDepMode, OHConfig.db.dependMode)
	end)

	local text = frame:CreateFontString("OHConfigUIDepModeText", frame, "GameFontNormalSmall")
	text:SetPoint("LEFT", self.dependMode, "RIGHT", 120, 3)
	text:SetText(L["DEP_ENABLE"])
	
	-- DROPDOWN FOR CHILDREN
	self.childMode = CreateFrame("Frame", "OHConfigUIChildMode", frame, "UIDropDownMenuTemplate")
	self.childMode:SetPoint("TOPLEFT", -10, -170)
	self.childMode:SetScript("OnShow", function(self)
		UIDropDownMenu_Initialize(self, OHConfig.InitChildDropdown)
		UIDropDownMenu_SetSelectedValue(OHConfigUIChildMode, OHConfig.db.childMode)
	end)

	local text = frame:CreateFontString("OHConfigUIChildModeText", frame, "GameFontNormalSmall")
	text:SetPoint("LEFT", self.childMode, "RIGHT", 120, 3)
	text:SetText(L["CHILDREN_ENABLE"])
	
	return frame
end

function OHConfig:InitDepDropdown()
	UIDropDownMenu_AddButton({ value = "ask", text = L["ASK_ALWAYS"], arg1 = "dep", func = OHConfig.DropdownClicked })
	UIDropDownMenu_AddButton({ value = "yes", text = L["YES_ALWAYS"], arg1 = "dep", func = OHConfig.DropdownClicked })
	UIDropDownMenu_AddButton({ value = "no", text = L["NO_ALWAYS"], arg1 = "dep", func = OHConfig.DropdownClicked })
end

function OHConfig:InitChildDropdown()
	UIDropDownMenu_AddButton({ value = "ask", text = L["ASK_ALWAYS"], arg1 = "child", func = OHConfig.DropdownClicked })
	UIDropDownMenu_AddButton({ value = "yes", text = L["YES_ALWAYS"], arg1 = "child", func = OHConfig.DropdownClicked })
	UIDropDownMenu_AddButton({ value = "no", text = L["NO_ALWAYS"], arg1 = "child", func = OHConfig.DropdownClicked })
end

function OHConfig:DropdownClicked()
	if( this.arg1 == "dep" ) then
		UIDropDownMenu_SetSelectedValue(OHConfigUIDepMode, this.value)
		OHConfig.db.dependMode = this.value
	elseif( this.arg1 == "child" ) then
		UIDropDownMenu_SetSelectedValue(OHConfigUIChildMode, this.value)
		OHConfig.db.childMode = this.value
	end
end

local frame = CreateFrame("Frame")
frame:RegisterEvent("ADDON_LOADED")
frame:SetScript("OnEvent", function(self, event, addon)
	if( event == "ADDON_LOADED" and addon == "OptionHouse" ) then
		OHConfig.Enable(OHConfig)
	end
end)
